package ar.edu.unsam.algo3.plantasvszombies.ui.windows

import ar.edu.unsam.algo3.plantasvszombies.dominio.Fila
import ar.edu.unsam.algo3.plantasvszombies.dominio.Planta
import ar.edu.unsam.algo3.plantasvszombies.ui.ApplicationModels.FilaAppModel
import java.awt.Color
import org.uqbar.arena.bindings.NotNullObservable
import org.uqbar.arena.bindings.ObservableProperty
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.layout.HorizontalLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.Selector
import org.uqbar.arena.widgets.tables.Column
import org.uqbar.arena.widgets.tables.Table
import org.uqbar.arena.windows.Dialog
import org.uqbar.arena.windows.WindowOwner
import org.uqbar.commons.utils.Observable

@Observable
class FilaWindow extends Dialog<FilaAppModel> {

	new(WindowOwner parent, Fila unaFila) {
		super(parent, new FilaAppModel)
		modelObject.fila = unaFila
		modelObject.actualizar
	}

	override def createMainTemplate(Panel mainPanel) {
		title = "Plantas Vs Zombies"
		taskDescription = "Editar Fila"
		super.createMainTemplate(mainPanel)
	}

	override protected createFormPanel(Panel mainPanel) {
		val tituloPanel = new Panel(mainPanel)
		tituloPanel.setLayout(new ColumnLayout(1))

		val lblTitulo = new Label(tituloPanel).setText("Fila")
		lblTitulo.foreground = Color::BLUE
		lblTitulo.fontSize = 20
		lblTitulo.setWidth(450)

		val propiedadesPanel = new Panel(mainPanel)
		propiedadesPanel.setLayout(new HorizontalLayout)
		val tipo = new Label(propiedadesPanel).setText("Tipo: ")
		tipo.foreground = Color::BLUE

		val selectorTipo = new Selector(propiedadesPanel).allowNull(false)
		selectorTipo.setWidth(100)
		selectorTipo.allowNull(false)
		selectorTipo.bindValueToProperty("fila.jardinZen")
		selectorTipo.bindEnabledToProperty("activarCombo")
		selectorTipo.bindItems(new ObservableProperty(modelObject.fila.jardin, "tiposPosibles"))

		val abmPanel = new Panel(mainPanel)
		abmPanel.setLayout(new HorizontalLayout)
		val agregarPlanta = new Button(abmPanel).setCaption("Agregar Planta").onClick[|agregarPlanta]
		agregarPlanta.bindEnabled(new NotNullObservable("fila.jardinZen"))
		var eliminarPlanta = new Button(abmPanel).setCaption("Eliminar Planta").onClick[|eliminarPlanta]
		eliminarPlanta.bindEnabled(new NotNullObservable("plantaElegida"))

		val tblPanel = new Panel(mainPanel)
		tblPanel.setLayout(new ColumnLayout(1))
		this.createResultsGrid(mainPanel)
	}

	def agregarPlanta() {
		val ventana = new ObtenerPlantaWindow(this, modelObject)
		ventana.onAccept[|modelObject.plantar]
		ventana.open
	}

	def void eliminarPlanta() {
		modelObject.quitarPlanta()
	}

	override protected addActions(Panel actionsPanel) {
		var btnAceptar = new Button(actionsPanel).setCaption("Aceptar").onClick[|accept].setAsDefault.disableOnError

		new Button(actionsPanel) //
		.setCaption("Cancelar").onClick[|this.cancel]

		// Deshabilitar los botones si no hay ningún elemento seleccionado en la grilla.
		var elementSelected = new NotNullObservable("fila.jardinZen")
		btnAceptar.bindEnabled(elementSelected)
	}

	// *************************************************************************
	// ** Grilla de plantas
	// *************************************************************************
	def protected createResultsGrid(Panel mainPanel) {
		var table = new Table<Planta>(mainPanel, typeof(Planta))
		table.heigth = 158
		table.width = 450
		table.bindItemsToProperty("misPlantas")
		table.bindValueToProperty("plantaElegida")

		this.describeResultsGrid(table)
	}

	def void describeResultsGrid(Table<Planta> table) {
		new Column<Planta>(table).setTitle("Nombre").setFixedSize(100).bindContentsToProperty("nombre")

		new Column<Planta>(table).setTitle("Defensa").setFixedSize(55).bindContentsToProperty("defensa")

		new Column<Planta>(table).setTitle("Ataque").setFixedSize(55).bindContentsToProperty("ataque")

		new Column<Planta>(table).setTitle("Descripcion").setFixedSize(250).bindContentsToProperty("descripcion")
	}
}
